package com.sherllinhe.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.sherllinhe.common.context.LoginMemberContext;
import com.sherllinhe.business.domain.*;
import com.sherllinhe.business.req.TrainSeatQueryReq;
import com.sherllinhe.business.resp.TrainSeatQueryResp;
import com.sherllinhe.common.exception.BusinessException;
import com.sherllinhe.common.exception.BusinessExceptionEnum;
import com.sherllinhe.common.resp.PageResp;
import com.sherllinhe.common.util.SnowUtil;
import com.sherllinhe.business.mapper.TrainStationMapper;
import com.sherllinhe.business.req.TrainStationQueryReq;
import com.sherllinhe.business.req.TrainStationSaveReq;
import com.sherllinhe.business.resp.TrainStationQueryResp;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能：
 * 作者：Sherllin He
 * 日期：2025/5/7 16:48
 */
@Slf4j
@Service
public class TrainStationService {
    @Autowired
    private TrainStationMapper trainStationMapper;
    // 新增和编辑乘客
    public void save(TrainStationSaveReq req){
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);

        //判断是新增还是更新
        //trainStation.setMemberId(LoginMemberContext.getId());
        if(ObjUtil.isNull(trainStation.getId())) {
            //新增前查询唯一键是否存在
            TrainStation trainStationDB = selectByUnique(req.getTrainCode(), req.getIndex());
            if(ObjUtil.isNotNull(trainStationDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_INDEX_UNIQUE_ERROR);
            }
            trainStationDB = selectByUnique(req.getTrainCode(), req.getName());
            if(ObjUtil.isNotNull(trainStationDB)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_STATION_NAME_UNIQUE_ERROR);
            }
            //新增
            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        }else{
            //更新
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKey(trainStation);
        }


    }
    // 查询乘客列表
    public PageResp<TrainStationQueryResp> queryList(TrainStationQueryReq req){
        TrainStationExample example = new TrainStationExample();
        example.setOrderByClause("train_code asc,`index` asc");
        TrainStationExample.Criteria criteria = example.createCriteria();
        //if(ObjUtil.isNotNull(req.getMemberId())){
            //criteria.andMemberIdEqualTo(req.getMemberId());
        //}
        if(ObjUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        if(ObjUtil.isNotEmpty(req.getName())){
            criteria.andNameEqualTo(req.getName());
        }

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(example);

        PageInfo<TrainStation> pageInfo = new PageInfo<>(trainStationList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainStationQueryResp> list = BeanUtil.copyToList(trainStationList, TrainStationQueryResp.class);

        PageResp<TrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }
    //删除乘客
    public void delete(Long id){
        trainStationMapper.deleteByPrimaryKey(id);
    }

    //查询某车次的车站信息
    public PageResp<TrainStationQueryResp> selectByTrainCode(String trainCode, TrainStationQueryReq req){
        TrainStationExample example = new TrainStationExample();
        TrainStationExample.Criteria criteria = example.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);

        log.info("查询页码:{}",req.getPage());
        log.info("每页条数:{}",req.getSize());
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(example);

        PageInfo<TrainStation> pageInfo = new PageInfo<>(trainStationList);
        log.info("总条数:{}", pageInfo.getTotal());
        log.info("总页数:{}", pageInfo.getPages());

        List<TrainStationQueryResp> list = BeanUtil.copyToList(trainStationList, TrainStationQueryResp.class);

        PageResp<TrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setList(list);
        pageResp.setTotal(pageInfo.getTotal());

        return pageResp;
    }

    //  查询唯一键trainCode, index
    private TrainStation selectByUnique(String trainCode, Integer index) {
        TrainStationExample example = new TrainStationExample();
        example.createCriteria().andTrainCodeEqualTo(trainCode).andIndexEqualTo(index);
        List<TrainStation> list = trainStationMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else {
            return null;
        }
    }
    //  查询唯一键trainCode, name
    private TrainStation selectByUnique(String trainCode, String name) {
        TrainStationExample example = new TrainStationExample();
        example.createCriteria().andTrainCodeEqualTo(trainCode).andNameEqualTo(name);
        List<TrainStation> list = trainStationMapper.selectByExample(example);
        if(CollUtil.isNotEmpty(list)){
            return list.get(0);
        }else {
            return null;
        }
    }
    // 查询唯一键trainCode
    public List<TrainStation> selectByUnique(String trainCode) {
        TrainStationExample example = new TrainStationExample();
        example.setOrderByClause("`index` asc");
        example.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainStationMapper.selectByExample(example);
    }
}
